package com.li.controller;

import com.li.mapper.RoleDeptMapper;
import com.li.pojo.dto.DeptDTO;
import com.li.pojo.po.*;
import com.li.pojo.vo.DeptVO;
import com.li.pojo.vo.MenuVO;
import com.li.service.DeptService;
import com.li.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/system/dept")
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleDeptMapper roleDeptMapper;

    /**
     * 新增部门
     *
     * @param deptDTO
     * @return
     */
    @PostMapping
    public Result addDept(@RequestBody DeptDTO deptDTO) {
        log.info("新增：{}", deptDTO);
        deptService.addDept(deptDTO);
        return Result.success();
    }

    /**
     * 整体的部门树
     *
     * @param dept
     * @return
     */
    @GetMapping("/treeselect")
    public Result getTreeSelect(Dept dept) {
        DeptVO list = deptService.getTreeSelect();
        log.info("deptVO列表：{}", list);
        List<DeptVO> deptVOS = new ArrayList<>();
        deptVOS.add(list);
//        return new Result(200,"操作成功",list);
        return Result.success(deptVOS);
    }

    /**
     * todo: 条件查询
     * 获取部门列表
     *
     * @param deptName
     * @param status
     * @return
     */
    @GetMapping("/list")
    public Result getDeptList(String deptName, String status) {
        List<Dept> deptList = deptService.getDeptList(deptName, status);
        return Result.success(deptList);
    }

    /**
     * 根据部id回显
     *
     * @param deptId
     * @return
     */
    @GetMapping("/{deptId}")
    public Result getDeptById(@PathVariable Integer deptId) {
        Dept getDeptById = deptService.getById(deptId);
        return Result.success(getDeptById);
    }

    /**
     * 修改部门信息
     *
     * @param dept
     * @return
     */
    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {
        deptService.updateDept(dept);
        return Result.success();
    }

    /**
     * 根据id删除部门
     *
     * @param deptId
     * @return
     */
    @DeleteMapping("/{deptId}")
    public Result deleteById(@PathVariable Integer deptId) {
        deptService.deleteById(deptId);
        return Result.success();
    }

    /**
     * 获取对应角色部门列表树
     */
    @GetMapping("/roleDeptTreeselect/{roleId}")
    public RoleIdDeptsResult roleDeptTreeselect(@PathVariable Integer roleId) {
        Role role = roleService.getById(roleId);
        String deptIds = role.getDeptIds();
        System.err.println(deptIds);
//        List<String> list = new ArrayList<>();
//        if (deptIds != null) {
//            String[] split = deptIds.split(",");
//            list.addAll(Arrays.asList(split));
//        }
        String[] split = deptIds.split(",");
        DeptVO deptVOList = deptService.getTreeSelect();
        List<DeptVO> deptVOS = new ArrayList<>();
        deptVOS.add(deptVOList);
        return new RoleIdDeptsResult(200, split, deptVOS, "查询成功");
    }
}
